//
//  AppointmentViewController.m
//  mhome
//
//  Created by YaSha_Tom on 2018/12/4.
//  Copyright © 2018年 刘春雷. All rights reserved.
//

#import "AppointmentViewController.h"
#import "TextFileld.h"
#import "AddressPickView.h"
#import "ActionSheet.h"
#import "UIImage+LX.h"

@interface AppointmentViewController ()
@property (nonatomic,strong) TextFileld *textfile;
@property (nonatomic,strong) NSUserDefaults *defaults;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong) NSDictionary *styleLikesD;
@property (nonatomic,copy) NSString *province;
@property (nonatomic,copy) NSString *provinceCode;//装修地址省
@property (nonatomic,copy) NSString *city;
@property (nonatomic,copy) NSString *cityCode;//装修地址市
@property (nonatomic,copy) NSString *area;
@property (nonatomic,copy) NSString *areaCode;//装修地址地区
@property (nonatomic,copy) NSString *type;
@property (nonatomic,copy) NSString *username;
@property (nonatomic,strong) NSString *btnNum;
@property (nonatomic,retain) NSArray *arrayK;
@end

@implementation AppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.defaults = [NSUserDefaults standardUserDefaults];
    self.title = @"预约下单";
    self.navigationController.navigationBar.translucent = NO;//关掉半透明
    self.view.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];
    UIImageView *imgev = [[UIImageView alloc]initWithFrame:CGRects(60, 40, 255 , 60)];
    imgev.image = [UIImage imageNamed:@"ad_mhome"];
    [self.view addSubview:imgev];
    [self initUI];
    [self styleRequest];
    [self setupNavBar];
}
- (void)setupNavBar {
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage getImageWithColor:[UIColor qmui_colorWithHexString:@"4bccbc"]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)initUI{
    for (int i = 0; i < 2; i++) {
        self.textfile = [[TextFileld alloc]initWithFrame:CGRects(30, (130+50*i), 315, 40)];
        if(i == 1){
            self.textfile.text = [self.defaults stringForKey:@"mobile"];
            self.textfile.userInteractionEnabled = NO;
        }else{
            self.textfile.placeholder = @"＊您的称呼";
            self.textfile.tag = 10;
        }
        [self.textfile addTarget:self action:@ selector(chang:) forControlEvents:UIControlEventEditingChanged];//监听是否有文本输入
        [self.textfile.layer setCornerRadius:5.0];
        self.textfile.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.textfile];
    }
     NSArray *array = @[@"装修地址区域",@"选择风格",@"房屋详细情况"];
    for (int i = 0 ; i < 2; i++){
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.frame = CGRects(30, (230+ i*50), (375-60), 40);
        [self.button setTitle:array[i] forState:UIControlStateNormal];
        self.button.tag = i+5;
        self.button.backgroundColor = [UIColor blueColor];
        self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
        self.button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
        self.button.backgroundColor = [UIColor whiteColor];
        [self.button addTarget:self action:@selector(pick:) forControlEvents:UIControlEventTouchUpInside ];
        [self.button.layer setCornerRadius:5.0];
        [self.view addSubview:self.button];
    }
    UIButton *buttonorder = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonorder.frame = CGRects(30, 380, (375-60), 40);
    [buttonorder setTitle:@"预约下单" forState:UIControlStateNormal];
    
    
    buttonorder.backgroundColor = [UIColor colorWithRed:18.0/255.0 green:101.0/255.0 blue:157.0/255.0 alpha:1.0];
    [buttonorder addTarget:self action:@selector(order) forControlEvents:UIControlEventTouchUpInside ];
    [buttonorder.layer setCornerRadius:5.0];
    [self.view addSubview:buttonorder];
}

- (void)chang:(UITextField *) tf {
    self.username = tf.text;
}

- (void)pick:(UIButton *)sender{
    self.tabBarController.tabBar.hidden=YES;
    if(sender.tag == 5){
        AddressPickView *addressPickView = [AddressPickView shareInstance];
        [self.view addSubview:addressPickView];
        addressPickView.block1 = ^(NSString *ss){
            self.tabBarController.tabBar.hidden=NO;
            [sender setTitle:ss forState:UIControlStateNormal];
        };
        addressPickView.block = ^(NSString *provinc,NSString *cit,NSString *town,NSString *proCode,NSString *citCode,NSString *twnCode){
            self.province = provinc;
            self.city = cit;
            self.area = town;
            self.provinceCode = proCode;
            self.cityCode = citCode;
            self.areaCode = twnCode;
            NSLog(@"---%@----%@-----%@",proCode,citCode,twnCode);
           NSString  *address = [NSString stringWithFormat:@"%@  %@  %@",self.province,self.city,self.area];
            self.tabBarController.tabBar.hidden=NO;
            [sender setTitle:address forState:UIControlStateNormal];
        };
    }
    if(sender.tag == 6){
        NSArray *styleArray = [NSArray new];
        styleArray = [self.styleLikesD allValues];
        self.arrayK = [self.styleLikesD allKeys];
        ActionSheet *chooseView = [[ActionSheet alloc] initViewWithMultiPhone:styleArray title:nil];
        self.tabBarController.tabBar.hidden=NO;
        [chooseView show];
        chooseView.black = ^(NSString *str) {
            self.type = str;
            //设置SecondViewController⾥里边的block属性,
            [sender setTitle:str forState:UIControlStateNormal];
        };
        [self.view addSubview:chooseView];
    }
}

- (void)order {
    if(self.username != nil && self.province!= nil && self.type != nil){
        if ([self.type isEqualToString:@"艾美"]){
            self.btnNum=[NSString stringWithFormat:@"%@",[_arrayK objectAtIndex:0]];
        }else if([self.type isEqualToString:@"SOHO"]){
            self.btnNum=[NSString stringWithFormat:@"%@",[_arrayK objectAtIndex:1]];
        }else{
            self.btnNum=[NSString stringWithFormat:@"%@",[_arrayK objectAtIndex:2]];
        }
        NSMutableDictionary * dictionary=[[NSMutableDictionary alloc]init];
        [dictionary setValue:self.btnNum forKey:@"styleLike"];
        [dictionary setValue:self.username forKey:@"name"];
        [dictionary setValue:[self.defaults stringForKey:@"mobile"] forKey:@"mobile"];
        [dictionary setValue:@"50" forKey:@"reSource"];
        [dictionary setValue:self.province forKey:@"province"];
        [dictionary setValue:self.provinceCode forKey:@"provinceCode"];
        [dictionary setValue:self.city forKey:@"city"];
        [dictionary setValue:self.cityCode forKey:@"cityCode"];
        [dictionary setValue:self.area forKey:@"area"];
        [dictionary setValue:self.areaCode forKey:@"areaCode"];
        [dictionary setValue:@" " forKey:@"zxType"];
        [dictionary setValue:@" " forKey:@"type"];
        [dictionary setObject:@" " forKey:@"regIp"];
        [dictionary setValue:@" " forKey:@"budget"];
        [dictionary setValue:@"1543914126" forKey:@"planzxDataStr"];
        NSString *urlString = [NSString stringWithFormat:@"%@%@",Domain,bookingApi];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:urlString parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"str = %@",str);
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            if ([[NSString stringWithFormat:@"%@",dic[@"code"]] isEqualToString:@"1"]) {
                [QMUITips showInfo:@"您已预约下单成功" inView:self.view];
                
                for (TextFileld *find_textFile in self.view.subviews) {
                    if (find_textFile.tag == 10){
                        find_textFile.text = nil;
                    }
                }
                for (UIButton *find_button in self.view.subviews) {
                    if (find_button.tag == 5){
                         [find_button setTitle:@"装修地址区域" forState:UIControlStateNormal];
                    }else{
                        [find_button setTitle:@"选择风格" forState:UIControlStateNormal];
                    }
                }
            }else{
                [QMUITips showInfo:@"您在30天内已有意向提交" inView:self.view hideAfterDelay:1];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error = %@",error);
        }];
    }else {
        [QMUITips showInfo:@"请填写完整信息" inView:self.view hideAfterDelay:1];
    }

}

- (void)styleRequest {
    NSString *urlString = [NSString stringWithFormat:@"%@%@",Domain,getLikeStyleDicApi];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:@{} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@",responseObject);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSDictionary * dataD=[dic objectForKey:@"data"];
        self.styleLikesD=[dataD objectForKey:@"styleLikes"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
